﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Infragistics.WebUI.UltraWebGrid;
using Infragistics.WebUI.Misc;

namespace Com.App_Code
{
    public class BasePage : System.Web.UI.Page
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnError(EventArgs e)
        {
            foreach (string key in Session.Keys)
            {
                if (Session[key] == null)
                {
                    Response.Redirect("~/Expire.aspx");
                }
            }

            string errorInfo = this.Server.GetLastError().Message + "\n\r" + this.Server.GetLastError().StackTrace;
            //Session[SessionVari.Error] = errorInfo;

            Response.Redirect("~/ErrorInfo.aspx");

            base.OnError(e);
        }

        protected override void OnInit(EventArgs e)
        {
            this.Response.Expires = 0;

            foreach (string key in Session.Keys)
            {
                if (Session[key] == null)
                {
                    Response.Redirect("~/Expire.aspx");
                }
            }

            base.OnInit(e);

        }

        /// <summary>
        /// 提示信息
        /// </summary>
        /// <param name="_strMessage"></param>
        public void ShowAlert(string strMessage)
        {
            string script = "<script language='javascript' type='text/javascript'>alert(\"" + strMessage + "\")</script>";
            ClientScript.RegisterClientScriptBlock(this.GetType(), Guid.NewGuid().ToString(), script);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_strScript"></param>
        public void Script(string _strScript)
        {
            string script = "<script language='javascript' type='text/javascript'>" + _strScript + "</script>";
            ClientScript.RegisterClientScriptBlock(this.GetType(), Guid.NewGuid().ToString(), script);
        }

        /// <summary>
        /// Infragistics的UpdatePanel的信息提示
        /// </summary>
        /// <param name="_control"></param>
        /// <param name="_strMessage"></param>
        public void ShowAlertForWARP(WebAsyncRefreshPanel _control, string _strMessage)
        {
            Infragistics.WebUI.Shared.CallBackManager.AddScriptBlock(Page, _control, " <script>alert(\"" + _strMessage + "\"); </script>");
        }
        /// <summary>
        /// 微软的UpdatePanel的信息提示
        /// </summary>
        public void ShowAlertForUpdatePanel(string _strMessage)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "updateScript", "alert('" + _strMessage + "')", true);
        }

        /// <summary>
        /// 取得站点前缀，如 HTTP://localhost/Project
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        public string GetBaseURL()
        {
            return GetBaseURL(this.Request);
        }

        /// <summary>
        /// 取得站点前缀，如 HTTP://localhost/Project
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        public string GetBaseURL(System.Web.UI.Page _page)
        {
            return GetBaseURL(_page.Request);
        }
        /// <summary>
        /// 取得站点前缀，如 HTTP://localhost/Project
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        protected string GetBaseURL(HttpRequest request)
        {
            string _path = Request.ApplicationPath == "/" ? "/" : Request.ApplicationPath + "/";
            if (Request.IsSecureConnection)
            {
                return @"https://" + Request.Url.Authority + _path;
            }
            else
            {
                return @"http://" + Request.Url.Authority + _path;
            }
        }

        /// <summary>
        /// 添加普通列
        /// </summary>
        /// <param name="GridSrc"></param>
        /// <param name="HeaderText"></param>
        /// <param name="Key"></param>
        /// <param name="WidthPix"></param>
        /// <param name="ColumnPosition"></param>
        /// <param name="Hidden"></param>
        public void AddGridColumn(Infragistics.WebUI.UltraWebGrid.UltraWebGrid GridSrc, string HeaderText,
            string Key, int WidthPix, int ColumnPosition, bool Hidden)
        {
            GridSrc.Columns.Add(Key);
            GridSrc.Columns.FromKey(Key).HeaderText = HeaderText;
            GridSrc.Columns.FromKey(Key).CellStyle.VerticalAlign = VerticalAlign.Middle;
            GridSrc.Columns.FromKey(Key).HeaderStyle.VerticalAlign = VerticalAlign.Middle;

            GridSrc.Columns.FromKey(Key).Key = Key;
            GridSrc.Columns.FromKey(Key).BaseColumnName = Key;
            GridSrc.Columns.FromKey(Key).Width = System.Web.UI.WebControls.Unit.Pixel(WidthPix);
            GridSrc.Columns.FromKey(Key).Hidden = Hidden;
            GridSrc.Columns.FromKey(Key).Move(ColumnPosition);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="GridSrc"></param>
        /// <param name="HeaderText"></param>
        /// <param name="Key"></param>
        /// <param name="WidthPix"></param>
        /// <param name="ColumnPosition"></param>
        /// <param name="ImageURL"></param>
        public void AddGridImageColumn(Infragistics.WebUI.UltraWebGrid.UltraWebGrid GridSrc, string HeaderText,
            string Key, int WidthPix, int ColumnPosition, string ImageURL)
        {
            GridSrc.Columns.Add(Key);
            GridSrc.Columns.FromKey(Key).HeaderText = HeaderText;
            GridSrc.Columns.FromKey(Key).CellStyle.VerticalAlign = VerticalAlign.Middle;
            GridSrc.Columns.FromKey(Key).CellStyle.BackgroundImage = ImageURL;
            GridSrc.Columns.FromKey(Key).CellStyle.CustomRules = "background-position:center;Background-repeat:no-repeat";
            GridSrc.Columns.FromKey(Key).CellStyle.Cursor = Infragistics.WebUI.Shared.Cursors.Hand;
            GridSrc.Columns.FromKey(Key).HeaderStyle.VerticalAlign = VerticalAlign.Middle;

            GridSrc.Columns.FromKey(Key).Key = Key;
            GridSrc.Columns.FromKey(Key).BaseColumnName = Key;
            GridSrc.Columns.FromKey(Key).Width = System.Web.UI.WebControls.Unit.Pixel(WidthPix);
            GridSrc.Columns.FromKey(Key).Move(ColumnPosition);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="grid"></param>
        /// <param name="movenum"></param>
        /// <param name="clientgridname"></param>
        public void AddGridCheckColumn(Infragistics.WebUI.UltraWebGrid.UltraWebGrid grid, int movenum,
            string clientgridname)
        {
            grid.Bands[0].Columns.Add("CHECK", "<INPUT TYPE=checkbox ID=chkAll class= 'CheckBox' onclick='SelectAll(\"" + clientgridname + "\",this)'>");
            UltraGridColumn checkcolumn = grid.Bands[0].Columns.FromKey("CHECK");
            checkcolumn.Type = Infragistics.WebUI.UltraWebGrid.ColumnType.CheckBox;
            checkcolumn.AllowUpdate = AllowUpdate.Yes;
            checkcolumn.AllowResize = AllowSizing.Fixed;
            checkcolumn.Width = new Unit(26);
            checkcolumn.CellStyle.HorizontalAlign = HorizontalAlign.Center;
            checkcolumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            checkcolumn.HeaderStyle.VerticalAlign = VerticalAlign.Middle;
            checkcolumn.CellStyle.VerticalAlign = VerticalAlign.Middle;
            checkcolumn.HeaderStyle.Padding.Top = 0;
            checkcolumn.HeaderStyle.Padding.Left = 0;
            checkcolumn.CellStyle.Padding.Top = 0;
            checkcolumn.CellStyle.Padding.Left = 0;
            checkcolumn.Move(movenum);


        }

        /// <summary>
        /// 添加普通列
        /// </summary>
        /// <param name="GridSrc"></param>
        /// <param name="HeaderText"></param>
        /// <param name="Key"></param>
        /// <param name="WidthPix"></param>
        /// <param name="ColumnPosition"></param>
        /// <param name="Hidden"></param>
        public void AddGridLinkColumn(Infragistics.WebUI.UltraWebGrid.UltraWebGrid GridSrc, string HeaderText,
            string Key, int WidthPix, int ColumnPosition, bool Hidden)
        {
            GridSrc.Columns.Add(Key);
            GridSrc.Columns.FromKey(Key).HeaderText = HeaderText;
            GridSrc.Columns.FromKey(Key).CellStyle.VerticalAlign = VerticalAlign.Middle;
            GridSrc.Columns.FromKey(Key).HeaderStyle.VerticalAlign = VerticalAlign.Middle;

            GridSrc.Columns.FromKey(Key).Key = Key;
            GridSrc.Columns.FromKey(Key).BaseColumnName = Key;
            GridSrc.Columns.FromKey(Key).Width = System.Web.UI.WebControls.Unit.Pixel(WidthPix);
            GridSrc.Columns.FromKey(Key).Hidden = Hidden;
            GridSrc.Columns.FromKey(Key).Move(ColumnPosition);

            GridSrc.Columns.FromKey(Key).CellStyle.ForeColor = System.Drawing.Color.Blue;
            GridSrc.Columns.FromKey(Key).CellStyle.Font.Underline = true;
            GridSrc.Columns.FromKey(Key).CellStyle.Cursor = Infragistics.WebUI.Shared.Cursors.Hand;
        }
    }
}